package com.demo.example.hot100;

public class MinPathSum {
    public static int min;
    public static void main(String[] args) {
        int[][] array=new int[][]{
                {1,3,1},
                {1,5,1},
                {4,2,1}
        };
        int[][] array2=new int[][]{
                {0,0},
                {0,0}
        };
        System.out.println(minPathSum02(array2));
    }
    //用动态规划才是最优解
    public static int minPathSum02(int[][] grid){
        //[[0]]
        if(grid.length==1 && grid[0].length==1){
            return grid[0][0];
        }
            int m=grid.length;
            int n=grid[0].length;
            int[][] dp=new int[m][n];
            dp[0][0]=grid[0][0];
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    //先做第一个判断，查看当前的数是否不为零
                    if(i==0 && j==0){
                        continue;
                    }
                    if(i==0){
                        dp[i][j]=dp[i][j-1]+grid[i][j];
                        continue;
                    }
                    if(j==0){
                        dp[i][j]=dp[i-1][j]+grid[i][j];
                        continue;
                    }
                    dp[i][j]=Math.min(dp[i-1][j]+grid[i][j],dp[i][j-1]+grid[i][j]);
                }
            }
            return dp[m-1][n-1];
    }
    public static int minPathSum(int[][] grid){
      //傻瓜法
        min=Integer.MAX_VALUE;
        process(grid,grid.length,grid[0].length,0,0,0);
        return min+grid[grid.length-1][grid[0].length-1];
    }
    public static void process(int[][] grid,int m,int n,int x,int y,int now){
        if(x>=m || y>=n){
            return;
        }
        if(x==m-1 && y==n-1){
            min=min<now?min:now;
            return;
        }
        process(grid,m,n,x+1,y,now+grid[x][y]);
        process(grid,m,n,x,y+1,now+grid[x][y]);
    }
}
